/* This class holds a Galaxy which is a group of sectors
 * 
 */

package map;

import game.Player;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Galaxy implements Serializable{

	private static final long serialVersionUID = 2759856985381460166L;
	private static final int GALAXY_ROWS = 6; //the number of rows int the sector
	private static final int GALAXY_COLS = 11; //the number of the columns in the sector
	private static final int NUM_SECTORS = 12;
	private ArrayList<Sector> sectors; //the list of nodes in the sector
	private char[][] galaxy; //holds a representation of the nodes
	private int galaxyNumber;


	//0 param constructor
	public Galaxy(int g){

		galaxyNumber = g;
		sectors = new ArrayList<Sector>(NUM_SECTORS);
		
		for(int i = 0; i <NUM_SECTORS; i++)
			sectors.add(null);		
		
		galaxy = new char[GALAXY_ROWS][GALAXY_COLS];
		Random r = new Random(); //used for generating the locations of the nodes

		//start off by setting the sector to an empty sector
		for(int i = 0; i < GALAXY_ROWS; i++){
			for(int j = 0; j < GALAXY_COLS; j++){

				galaxy[i][j] = '-';
			}//for
		}//for

		//insert the Entrance and Exit nodes
		int row = r.nextInt(GALAXY_ROWS / 2 + 1) + GALAXY_ROWS / 4; //used to hold randomly generated rows
		int col = r.nextInt(GALAXY_ROWS / 2 + 1) + GALAXY_ROWS / 4; //used to hold randomly generated cols
		galaxy[row][0] = 'B';
		galaxy[col][GALAXY_COLS - 1] = 'E'; //I use col here because I need two random numbers and needed to keep track of both

		sectors.set(0, new Sector(row, 0));
		sectors.set(NUM_SECTORS - 1, new Sector(col, GALAXY_COLS - 1));

		//generates 8 non "door" nodes. These nodes cannot be in the same column as the entrance and exit nodes but can be anywhere else
		//nodes are also not allowed to be directly adjacent to any other node in any of the 8 directions.
		for(int i = 1; i <= NUM_SECTORS - 2; i++){

			boolean cont = true; //holds of another set of coordinates need to be generated
			row = 0;
			col = 0;

			//keeps on generating nodes until a valid one is found
			do{
				row = r.nextInt(GALAXY_ROWS);
				col = r.nextInt(GALAXY_COLS - 2) + 1;

				//checks to see if that space is already occupied
				while(galaxy[row][col] == 'N'){				

					row = r.nextInt(GALAXY_ROWS);
					col = r.nextInt(GALAXY_COLS - 2) + 1;
				}//while

				//Check all eight directions in a clockwise direction starting with left.
				if(galaxy[row][col - 1] == '-')
					if(row - 1 == -1 || galaxy[row - 1][col - 1] == '-')
						if(row - 1 == -1 || galaxy[row - 1][col] == '-')
							if(row - 1 == -1 || galaxy[row - 1][col + 1] == '-')
								if(galaxy[row][col + 1] == '-')
									if(row + 1 == GALAXY_ROWS || galaxy[row + 1][col + 1] == '-')
										if(row + 1 == GALAXY_ROWS || galaxy[row + 1][col] == '-')
											if(row + 1 == GALAXY_ROWS || galaxy[row + 1][col - 1] == '-')
												cont = false;


			}while(cont);

			//places the node after all checks are made
			galaxy[row][col] = 'S';
			sectors.set(i, new Sector(row, col));
		}//for
		
		Collections.sort(sectors);
	}//default constructor
	
	
	//generates and returns the sector at the given location
	public Sector explore(int r, int c, int l){
		
		int index = 0; //holds the index of the current Sector being examined
		Sector s = null;
		
		while(index < GALAXY_COLS &&sectors.get(index).getCol() <= c && s == null){
			
			if(sectors.get(index).getCol() == c && sectors.get(index).getRow() == r)
				s = sectors.get(index);
		}//while
		
		if(s != null){
			
		
			if(c == GALAXY_COLS - 1)
				s.generate(galaxyNumber * 9, true);
			
			else
				s.generate(l);
		}//if
		
		return s;
	}//explore


	//returns a string representation of the sector
	public String toString(){

		String toReturn = "  0 1 2 3 4 5 6 7 8 9\n"; //the string that is returned

		for(int i = 0; i < GALAXY_ROWS; i++){

			toReturn += i + "";

			for(int j = 0; j < GALAXY_COLS; j++){

				toReturn += " " + galaxy[i][j];				
			}//for

			toReturn += "\n";
		}//for		
		return toReturn;
	}//toString
	
	
	//main method used for testing
	public static void main(String[] args){
		
		for(int i = 0; i < 100; i++){
			
			System.out.println(new Galaxy(i));
		}//for

	}//main
	
}//Galaxy